#
# Copyright 2012 ibiblio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
import ConfigParser

"""
Default list when looking for a configuration file.  The first match
is read by the ConfigHelper.
"""
CONFIG_FILENAME = 'seedbank.conf'
CONFIG_SEARCH_DEFAULT = [
    CONFIG_FILENAME,
    'conf/' + CONFIG_FILENAME,
    os.path.dirname(__file__) + '/../../../conf/' + CONFIG_FILENAME,
    '/usr/local/seedbank/etc/' + CONFIG_FILENAME,
    '/etc/seedbank/' + CONFIG_FILENAME
    ]

MAIN_SECTION = 'main'
UPLOAD_SECTION = 'upload'
SEEDBANKDB_SECTION = 'seedbank_db'
MQ_SECTION = 'message_queue'
CREATE_TORRENT_SECTION = 'create_torrent'

"""
Default configuration values
"""
DEFAULT_VALUES = {
    MAIN_SECTION: {
        'torrent_file_root': '/var/lib/seedbank/torrents',
        'listen_address': '0.0.0.0',
        'listen_port': '6881',
        'error_log': '/var/log/seedbank/error.log',
        'log_level': 'info',
        'inactive_torrent_timeout': 60,
        'torrent_manager_exec_interval': 5,
        'data_volume_root': '/var/lib/seedbank/data'
        },
    SEEDBANKDB_SECTION: {
        'db_host': 'localhost',
        'db_port': '27017',
        'db_name': 'seedbank',
        'db_user': '',
        'db_pass': ''
        },
    MQ_SECTION: {
        'host': 'localhost',
        'port': '5672',
        'user': 'terasaur',
        'pass': 'terasaur',
        'vhost': '/terasaur',
        'exchange': 'terasaur',
        'control_queue': 'seedbank.control',
        'stats_queue': 'seedbank.stats',
        'terasaur_queue': 'terasaur.web'
        },
    CREATE_TORRENT_SECTION: {
        'tracker_url': 'http://tracker.ibiblio.org/announce'
        }
    }

class ConfigHelper(object):
    def __init__(self, file_list):
        self._config_file = self._find_file(file_list)
        self._config = None

    def _find_file(self, file_list):
        found = ''
        for file_path in file_list:
            if os.path.exists(file_path):
                found = os.path.realpath(file_path)
                break
        return found

    def get_config(self):
        if not self._config:
            self._config = self._read_config(self._config_file)
        return self._config

    def reset(self):
        self._config = None

    def _read_config(self, config_file):
        config = self._get_config_with_defaults()
        config.read(config_file)
        return config

    def _get_config_with_defaults(self):
        config = ConfigParser.RawConfigParser()
        for section in DEFAULT_VALUES:
            config.add_section(section)
            for k,v in DEFAULT_VALUES[section].iteritems():
                config.set(section, k, v)
        return config
